package controllers;

import java.util.List;

import models.Menu;
import play.cache.Cache;
import play.mvc.Controller;

import com.etaoking.commons.MenuUtils;
import com.etaoking.commons.TaobaoUtils;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.TaobaokeItem;

public class TaoKeAction extends Controller {
	public static void index() {
//		List<Menu> menus = MenuUtils.getInstance().getMenus();
		List<ItemCat> itemCats = TaobaoUtils.getRootItemCats();
		List<TaobaokeItem> taobaokeItems = Cache.get("taobaokeItems",
				List.class);
		if (taobaokeItems == null) {
			taobaokeItems = TaobaoUtils.getTaobaokeItems(16L, 0L, 40L);
			Cache.set("taobaokeItems", taobaokeItems, "30mn");
		}
//		render(itemCats, taobaokeItems, menus);
		render(itemCats, taobaokeItems);
	}

	/**
	 * 选择分类
	 * 
	 * @param cid
	 *            分类id
	 */
	public static void selectItemCat(Long cid) {
//		List<Menu> menus = MenuUtils.getInstance().getMenus();
		List<TaobaokeItem> taobaokeItems = Cache.get(cid + "_taobaokeItems",
				List.class);
		if (taobaokeItems == null) {
			taobaokeItems = TaobaoUtils.getTaobaokeItems(cid, 0L, 40L);
			Cache.set(cid + "_taobaokeItems", taobaokeItems, "30mn");
		}
		List<ItemCat> itemCats = TaobaoUtils.getRootItemCats();
		String title = "Home";
		for (ItemCat itemCat : itemCats) {
			if (itemCat.getCid().equals(cid)) {
				title = itemCat.getName();
				break;
			}
		}
//		render(itemCats, taobaokeItems, title, menus);
		render("TaoKeAction/index.html",itemCats, taobaokeItems, title);
	}

	public static void doc() {
		render();
	}
}
